package com.huawei.appmarket.service.predownload.thread;

import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.net.NetworkRequest.Builder;
import android.os.Build.VERSION;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.downloadengine.api.DownloadChkInfo;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.downloadengine.api.SplitTask;
import com.huawei.appgallery.foundation.application.pkgmanage.model.update.ApkUpgradeInfo;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.wishlist.api.RealizedWishInfo;
import com.huawei.appmarket.service.deamon.bean.DownloadHistory;
import com.huawei.appmarket.service.deamon.bean.StartDownloadResponse;
import com.huawei.hms.framework.network.restclient.hianalytics.HianalyticsData;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import com.huawei.updatesdk.service.otaupdate.UpdateKey;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import o.bmt;
import o.bmw;
import o.bmx;
import o.cdb;
import o.cdf;
import o.cdg;
import o.cdl;
import o.ceq;
import o.cfw;
import o.cnt;
import o.cnw;
import o.coc;
import o.cof;
import o.coi;
import o.cvt;
import o.dgh;
import o.dhq;
import o.djy;
import o.dkb;
import o.dkc;
import o.Logger;
import o.doo;
import o.ContentManager;
import o.dss;
import o.dsv;
import o.dvk;
import o.dvo;
import o.dvq;
import o.dvt;
import o.dvy;
import o.edh;
import o.edj;
import o.edk;
import o.edl;
import o.edm;
import o.edn;
import o.edp;
import o.edt;
import o.edw;
import o.edz;
import o.eea;
import o.eeb;
import o.epp;
import o.CollectCheck;
import o.eqp;
import o.etn;
import o.eub;
import o.euc;
import o.ActivityUtil;
import o.fho;
import o.fhz;

public class PreDownloadManagerThread {
    public static final Map<Integer, eeb> ˊ = new ConcurrentHashMap();
    private boolean ʻ;
    private b ʼ;
    private int ʽ;
    private List<? extends ApkUpgradeInfo> ˋ;
    private int ˋॱ;
    private Context ˎ;
    bmt ˏ;
    private boolean ͺ;
    private final e ॱ;
    private long ॱॱ;
    private d ᐝ;

    public static class ApkUpgradeInfoComparator extends ApkUpgradeInfo {
        private static final long serialVersionUID = 6671720917146434403L;

        public /* synthetic */ int compare(Object obj, Object obj2) {
            return ॱ((ApkUpgradeInfo) obj, (ApkUpgradeInfo) obj2);
        }

        private long ˊ(long j, long j2) {
            return j2 > 0 ? j2 : j;
        }

        public int ॱ(ApkUpgradeInfo apkUpgradeInfo, ApkUpgradeInfo apkUpgradeInfo2) {
            if ("com.huawei.hwid".equals(apkUpgradeInfo.ᐝ())) {
                return -1;
            }
            if ("com.huawei.hwid".equals(apkUpgradeInfo2.ᐝ())) {
                return 1;
            }
            if (apkUpgradeInfo.ᐝ().equals(ContentManager.getInstant().getContext().getPackageName())) {
                return 1;
            }
            if (apkUpgradeInfo2.ᐝ().equals(ContentManager.getInstant().getContext().getPackageName())) {
                return -1;
            }
            long ˉ = ((long) edl.ˏ().ˉ()) * 1048576;
            long ˊ = ˊ(apkUpgradeInfo.ʻॱ(), apkUpgradeInfo.ʼ());
            long ˊ2 = ˊ(apkUpgradeInfo2.ʻॱ(), apkUpgradeInfo2.ʼ());
            if (ˊ < ˉ && ˊ2 > ˉ) {
                return -1;
            }
            if (ˊ > ˉ && ˊ2 < ˉ) {
                return 1;
            }
            if (apkUpgradeInfo.ˋˋ() > apkUpgradeInfo2.ˋˋ()) {
                return -1;
            }
            if (apkUpgradeInfo.ˋˋ() < apkUpgradeInfo2.ˋˋ()) {
                return 1;
            }
            return super.ॱ(apkUpgradeInfo, apkUpgradeInfo2);
        }
    }

    static final class b extends BroadcastReceiver {
        b() {
        }

        public void onReceive(Context context, Intent intent) {
            PreDownloadManagerThread.ˋ(context);
        }
    }

    class c implements dkc {
        private boolean ˊ = false;
        private long ˋ = 0;
        private final long ˎ = System.currentTimeMillis();
        final /* synthetic */ PreDownloadManagerThread ˏ;

        c(PreDownloadManagerThread preDownloadManagerThread) {
            this.ˏ = preDownloadManagerThread;
        }

        public void ॱ(SessionDownloadTask sessionDownloadTask) {
            euc.ˊ().ॱ();
            switch (sessionDownloadTask.ˈ()) {
                case 1:
                    this.ˋ = sessionDownloadTask.ˋˊ();
                    break;
                case 2:
                    ˎ(sessionDownloadTask);
                    break;
                case 3:
                    Logger.ˎ("PreDlManThd", "cancel download and delete the task from database");
                    djy.ॱ(1);
                    break;
                case 4:
                    ˋ(sessionDownloadTask);
                    break;
                case 5:
                    ˊ(sessionDownloadTask);
                    break;
                case 6:
                    ˏ(sessionDownloadTask);
                    break;
            }
            euc.ˊ().ˏ();
        }

        private void ˎ(SessionDownloadTask sessionDownloadTask) {
            if (edh.ˋ(ContentManager.getInstant().getContext()).ॱ || this.ˏ.ˋॱ == 4 || this.ˏ.ˋॱ == -1) {
                Logger.ˎ("PreDlManThd", "auto-downloading is started by power connected or open app, can not add to cache.");
            } else {
                long ˋˊ = sessionDownloadTask.ˋˊ() - this.ˋ;
                if (ˋˊ > 0) {
                    edn.ˋ().ˋ(ˋˊ);
                }
                this.ˋ = sessionDownloadTask.ˋˊ();
            }
            if (this.ˊ) {
                ॱॱ(sessionDownloadTask);
            } else if (System.currentTimeMillis() - this.ˎ > 300) {
                this.ˊ = true;
            }
        }

        private void ˋ(SessionDownloadTask sessionDownloadTask) {
            DownloadHistory downloadHistory = new DownloadHistory(sessionDownloadTask);
            downloadHistory.ॱ();
            dsv.ॱ(downloadHistory);
            int ˊ = this.ˏ.ॱ.ˊ(sessionDownloadTask);
            Intent intent = new Intent();
            intent.setAction(dvq.ˊ());
            this.ˏ.ˎ.sendBroadcast(intent);
            dss.ॱ().ˏ(sessionDownloadTask.ˊˊ());
            if (!TextUtils.isEmpty(sessionDownloadTask.ॱˋ())) {
                if (dhq.ॱ().ˎ()) {
                    cfw.ˎ(new dvk(sessionDownloadTask, 0), new eea());
                } else {
                    Logger.ˎ("PreDlManThd", "download successed, can not run bkg report");
                }
            }
            Logger.ˎ("PreDlManThd", "downloaded ,name:" + sessionDownloadTask.ʿ());
            Logger.ˎ("PreDlManThd", "downloaded and delete the poor network download pause record!");
            edk.ˏ().ॱ(sessionDownloadTask.ˊˊ());
            ApkUpgradeInfo ˎ = this.ˏ.ʽ(sessionDownloadTask);
            if (((ceq) cdb.ॱ(ceq.class)).ˊ(this.ˏ.ˎ, sessionDownloadTask.ˊˊ())) {
                Logger.ˎ("PreDlManThd", "start installing the app");
                this.ˏ.ˏ(sessionDownloadTask, ˊ, ˎ);
            } else {
                Logger.ˎ("PreDlManThd", "The device didn't have enough storage space to install the app");
                this.ˏ.ˏ(sessionDownloadTask, ˎ, 0);
            }
            djy.ॱ(1);
        }

        private void ˊ(SessionDownloadTask sessionDownloadTask) {
            if (!TextUtils.isEmpty(sessionDownloadTask.ॱˋ())) {
                if (dhq.ॱ().ˎ()) {
                    cfw.ˎ(new dvk(sessionDownloadTask, -1), new eea());
                } else {
                    Logger.ˎ("PreDlManThd", "download failed, can not run bkg report");
                }
            }
            Logger.ˎ("PreDlManThd", "download failed, record poor network download!");
            edk.ˏ().ˏ(sessionDownloadTask.ˊˊ());
            djy.ॱ(1);
        }

        private void ˏ(SessionDownloadTask sessionDownloadTask) {
            if (sessionDownloadTask.ˏˎ()) {
                Logger.ˎ("PreDlManThd", "Download paused because failed, record poor network download!");
                edk.ˏ().ˏ(sessionDownloadTask.ˊˊ());
            }
            if (sessionDownloadTask.interruptReason_ == 1) {
                this.ˏ.ͺ = true;
                edn.ˋ().ˎ();
            }
            djy.ॱ(1);
        }

        private void ॱॱ(SessionDownloadTask sessionDownloadTask) {
            this.ˏ.ʽ = (int) this.ˏ.ˏ.ˋ();
            if (this.ˏ.ˏ.ˏ() == bmw.POOR) {
                this.ˏ.ʻ = true;
            }
            if (Logger.ˎ()) {
                Logger.ˏ("PreDlManThd", "get network speed, is poor network: " + this.ˏ.ʻ + ", average speed: " + this.ˏ.ʽ);
            }
            if (this.ˏ.ʻ) {
                dvt.ˏ().ˏ(sessionDownloadTask.ʾ(), sessionDownloadTask.ˊˊ(), 7);
            }
        }
    }

    @TargetApi(21)
    static final class d extends NetworkCallback {
        d() {
        }

        public void onAvailable(Network network) {
            super.onAvailable(network);
            PreDownloadManagerThread.ˋ(ContentManager.getInstant().getContext());
        }

        public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
            super.onCapabilitiesChanged(network, networkCapabilities);
            PreDownloadManagerThread.ˋ(ContentManager.getInstant().getContext());
        }

        public void onLost(Network network) {
            super.onLost(network);
            PreDownloadManagerThread.ˋ(ContentManager.getInstant().getContext());
        }
    }

    public interface e {
        void ʼ();

        int ˊ(@NonNull SessionDownloadTask sessionDownloadTask);

        void ˊ(int i);

        void ˊ(@Nullable SessionDownloadTask sessionDownloadTask, @NonNull ApkUpgradeInfo apkUpgradeInfo, o.dkb.d dVar);

        void ˊ(List<String> list);

        Comparator<ApkUpgradeInfo> ˋ();

        boolean ˋ(SessionDownloadTask sessionDownloadTask);

        boolean ˎ();

        void ˏ();

        int ॱ();
    }

    public PreDownloadManagerThread(Context context, List<? extends ApkUpgradeInfo> list, @NonNull e eVar) {
        this.ॱॱ = 0;
        this.ʻ = false;
        this.ʽ = 0;
        this.ͺ = false;
        this.ˋॱ = -1;
        this.ˏ = new bmt();
        this.ˋ = list;
        this.ˎ = context;
        this.ॱ = eVar;
    }

    public PreDownloadManagerThread(Context context, int i, List<? extends ApkUpgradeInfo> list, @NonNull e eVar) {
        this(context, list, eVar);
        this.ˋॱ = i;
    }

    public void ॱ() {
        ᐝ();
        List ˋ = ˋ();
        Comparator ˋ2 = this.ॱ.ˋ();
        if (ˋ2 != null) {
            Collections.sort(this.ˋ, ˋ2);
        }
        ˋ(ˋ);
        if (dhq.ॱ().ˎ()) {
            int ˍ = edl.ˏ().ˍ();
            int ˎˎ = edl.ˏ().ˎˎ();
            while (dhq.ॱ().ˎ() && ˎ(ˍ, ˎˎ)) {
                SessionDownloadTask sessionDownloadTask = (SessionDownloadTask) edp.ˏ().ˋ().get(0);
                if (ॱ(sessionDownloadTask)) {
                    try {
                        ˊ();
                        ˊ(sessionDownloadTask);
                    } catch (OutOfMemoryError e) {
                        Logger.e("PreDlManThd", "predownload error!!!");
                    } finally {
                        ॱॱ();
                    }
                }
                edp.ˏ().ˋ().remove(0);
                if (edp.ˏ().ˋ().size() > 0) {
                    long ͺ = edl.ˏ().ͺ();
                    Logger.ˏ("PreDlManThd", "waiting, pauseTime:" + ͺ);
                    try {
                        Thread.sleep(ͺ);
                    } catch (InterruptedException e2) {
                        Logger.ˎ("PreDlManThd", "pause time Interrupted");
                    }
                }
            }
            ʻ();
        }
    }

    private static boolean ˎ(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivityManager == null) {
            return false;
        }
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo == null || !activeNetworkInfo.isConnected() || !activeNetworkInfo.isAvailable() || 1 != activeNetworkInfo.getType()) {
            Logger.ˎ("PreDlManThd", ".....network is not wifi");
            return false;
        } else if (!ˏ(context)) {
            return true;
        } else {
            Logger.ˎ("PreDlManThd", ".....network is MeteredHint");
            return false;
        }
    }

    private static void ˋ(Context context) {
        if (!ˎ(context)) {
            SessionDownloadTask ˎ = edp.ˏ().ˎ();
            if (ˎ != null) {
                dvt.ˏ().ˏ(ˎ.ʾ(), ˎ.ˊˊ(), 7);
                Logger.ˎ("PreDlManThd", "net change to no wifi, stop the download, pkg: " + ˎ.ˊˊ());
            }
        }
    }

    private static boolean ˏ(Context context) {
        return doo.ᐝ(context);
    }

    private void ˋ(List<SessionDownloadTask> list) {
        int size = this.ˋ.size();
        for (int i = 0; i < size; i++) {
            ApkUpgradeInfo apkUpgradeInfo = (ApkUpgradeInfo) this.ˋ.get(i);
            if (((cdg) cdb.ॱ(cdg.class)).ʼ(apkUpgradeInfo.ᐝ())) {
                Logger.ˎ("PreDlManThd", "not need predownload, ignores app:" + apkUpgradeInfo.ᐝ());
            } else if (((cdg) cdb.ॱ(cdg.class)).ʽ(apkUpgradeInfo.ᐝ())) {
                Logger.ˎ("PreDlManThd", "no need preddownload,stop app:" + apkUpgradeInfo.ᐝ());
            } else if (TextUtils.isEmpty(apkUpgradeInfo.ˈ())) {
                Logger.ˎ("PreDlManThd", "not need predownload, downUrl is empty");
            } else if (eub.ˎ().ॱ(apkUpgradeInfo.ᐝ(), -1) == apkUpgradeInfo.ʿ()) {
                Logger.ˎ("PreDlManThd", "not need predownload,this version has install failed!" + apkUpgradeInfo.ᐝ());
            } else if (apkUpgradeInfo.ʻॱ() <= 0) {
                Logger.e("PreDlManThd", "not need predownload,this app do not have size!");
            } else {
                int ˋ = ((cdg) cdb.ॱ(cdg.class)).ˋ(apkUpgradeInfo.ᐝ());
                if (ˋ != 1 && ˋ != 2) {
                    ॱ((List) list, apkUpgradeInfo);
                } else if (((ceq) cdb.ॱ(ceq.class)).ˊ(this.ˎ, apkUpgradeInfo.ᐝ(), apkUpgradeInfo.ʿ())) {
                    ˏ(apkUpgradeInfo);
                } else {
                    ((cdf) cdb.ॱ(cdf.class)).ॱ(apkUpgradeInfo.ᐝ());
                }
            }
        }
    }

    private void ॱ(List<SessionDownloadTask> list, final ApkUpgradeInfo apkUpgradeInfo) {
        SessionDownloadTask ˎ = edw.ˎ((List) list, apkUpgradeInfo.ᐝ());
        if (ˎ == null) {
            SessionDownloadTask ˋ = dvt.ˏ().ˋ(apkUpgradeInfo.ᐝ());
            if (ˋ != null) {
                Logger.ˎ("PreDlManThd", apkUpgradeInfo.ᐝ() + " already exist other dlType:" + ˋ.ˌ());
                return;
            }
        }
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        this.ॱ.ˊ(ˎ, apkUpgradeInfo, new o.dkb.d(this) {
            final /* synthetic */ PreDownloadManagerThread ॱ;

            public void ˋ(@Nullable SessionDownloadTask sessionDownloadTask) {
                if (sessionDownloadTask != null) {
                    Logger.ˎ("PreDlManThd", "buildDownloadTask task not null");
                    if (apkUpgradeInfo instanceof RealizedWishInfo) {
                        Logger.ˎ("PreDlManThd", "buildDownloadTask RealizedWishInfo set wish id.");
                        sessionDownloadTask.ˋ(((RealizedWishInfo) apkUpgradeInfo).ॱʼ());
                    }
                    if (!this.ॱ.ˎ(apkUpgradeInfo, sessionDownloadTask)) {
                        edp.ˏ().ˋ().add(sessionDownloadTask);
                    }
                }
                Logger.ˎ("PreDlManThd", "buildDownloadTask countDown");
                countDownLatch.countDown();
            }
        });
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            Logger.ˋ("PreDlManThd", "InterruptedException");
        }
    }

    private void ˏ(ApkUpgradeInfo apkUpgradeInfo) {
        edw.ˏ(apkUpgradeInfo, this.ॱ.ॱ());
    }

    private boolean ˎ(ApkUpgradeInfo apkUpgradeInfo, SessionDownloadTask sessionDownloadTask) {
        if (dkb.ˏ(sessionDownloadTask) <= 0) {
            Logger.e("PreDlManThd", "can not add Task to PreDownloadTaskManager,because size is empty.");
            return true;
        }
        List<SplitTask> ᐝˊ = sessionDownloadTask.ᐝˊ();
        if (!CollectCheck.isEmpty((List) ᐝˊ)) {
            for (SplitTask ʼ : ᐝˊ) {
                if (ʼ.ʼ() <= 0) {
                    String str = apkUpgradeInfo.ᐝ() + '|' + apkUpgradeInfo.ʼ() + '|' + apkUpgradeInfo.ʻॱ();
                    Logger.e("PreDlManThd", "online size error:" + str);
                    dgh.ॱ(ContentManager.getInstant().getContext(), "980201", str);
                    return true;
                }
            }
        }
        return false;
    }

    private boolean ॱ(SessionDownloadTask sessionDownloadTask) {
        if (!this.ॱ.ˋ(sessionDownloadTask)) {
            Logger.ˎ("PreDlManThd", "can not start download for online state is deny:" + sessionDownloadTask.ˊˊ());
            return false;
        } else if (!ˎ(sessionDownloadTask)) {
            dvt.ˏ().ˊ(sessionDownloadTask.ˊˊ());
            return false;
        } else if (((cdg) cdb.ॱ(cdg.class)).ʼ(sessionDownloadTask.ˊˊ())) {
            Logger.ˎ("PreDlManThd", "app has been ignored.");
            dvt.ˏ().ˊ(sessionDownloadTask.ˊˊ());
            return false;
        } else if (((cdg) cdb.ॱ(cdg.class)).ʽ(sessionDownloadTask.ˊˊ())) {
            Logger.ˎ("PreDlManThd", "app has been stop.");
            dvt.ˏ().ˊ(sessionDownloadTask.ˊˊ());
            return false;
        } else {
            List<SessionDownloadTask> ˏ = dvt.ˏ().ˏ(sessionDownloadTask.ˊˊ());
            if (!CollectCheck.isEmpty((List) ˏ)) {
                for (SessionDownloadTask ˋ : ˏ) {
                    if (dvt.ˏ().ˋ(ˋ)) {
                        Logger.ˎ("PreDlManThd", "has such forground task.");
                        dvt.ˏ().ˊ(sessionDownloadTask.ˊˊ());
                        return false;
                    }
                }
            }
            if (!ˎ(this.ˎ)) {
                Logger.ˎ("PreDlManThd", "net work is not wifi.");
                return false;
            } else if (!((cdg) cdb.ॱ(cdg.class)).ˎ(sessionDownloadTask.ˊˊ(), sessionDownloadTask.ॱᐝ())) {
                return true;
            } else {
                Logger.ˎ("PreDlManThd", "app has installed.");
                dvt.ˏ().ˊ(sessionDownloadTask.ˊˊ());
                return false;
            }
        }
    }

    @NonNull
    private List<SessionDownloadTask> ˋ() {
        List<SessionDownloadTask> arrayList = new ArrayList();
        int ॱ = this.ॱ.ॱ();
        for (SessionDownloadTask sessionDownloadTask : dvt.ˏ().ͺ()) {
            if (sessionDownloadTask.ˌ() == ॱ) {
                arrayList.add(sessionDownloadTask);
            }
        }
        return arrayList;
    }

    private boolean ˎ(SessionDownloadTask sessionDownloadTask) {
        long ˏ = dkb.ˏ(sessionDownloadTask) * 4;
        long ॱ = epp.ˎ(this.ˎ).ॱ();
        if (ॱ >= ˏ) {
            return true;
        }
        Logger.ˎ("PreDlManThd", "can not preDownload the app:" + sessionDownloadTask.ˊˊ() + ",free Space:" + ॱ);
        return false;
    }

    public static void ˋ(String str) {
        LinkedHashMap linkedHashMap = new LinkedHashMap(1);
        linkedHashMap.put("reason", str);
        cdl.ˎ("050", linkedHashMap);
    }

    private boolean ˎ(int i, int i2) {
        if (ˎ()) {
            Logger.ˎ("PreDlManThd", "end manager.....download data usage limited");
            return false;
        } else if (this.ͺ) {
            Logger.ˎ("PreDlManThd", "end manager.....user paused");
            ˋ("userPaused");
            return false;
        } else if (this.ʻ) {
            Logger.ˎ("PreDlManThd", "end manager.....poor network");
            ˋ("poorNet#" + this.ʽ);
            return false;
        } else if (dvt.ˏ().ॱॱ()) {
            Logger.ˎ("PreDlManThd", "end manager.....hispace is downloading");
            return false;
        } else if (dvt.ˏ().ᐝ()) {
            Logger.ˎ("PreDlManThd", "end manager.....hispace has prior task");
            return false;
        } else if (edp.ˏ().ˋ().size() <= 0) {
            Logger.ˎ("PreDlManThd", "end manager.....no preDownloads!");
            return false;
        } else if (this.ॱ.ˎ()) {
            ˏ(i, i2);
            if (ˎ(this.ˎ)) {
                return true;
            }
            Logger.ˎ("PreDlManThd", "end manager.....network is not wifi.");
            ˋ("noWifi");
            return false;
        } else {
            Logger.ˎ("PreDlManThd", "end manager.....Module Switch is closed!");
            return false;
        }
    }

    private boolean ˎ() {
        if (edh.ˋ(ContentManager.getInstant().getContext()).ॱ) {
            Logger.ˎ("PreDlManThd", "auto download data usage is not limited when charging");
            return false;
        } else if (this.ˋॱ == 4 || this.ˋॱ == -1) {
            Logger.ˎ("PreDlManThd", "auto download data usage is not limited when this download is started by opening app");
            return false;
        } else {
            long ˊˊ = ((long) edl.ˏ().ˊˊ()) * 1048576;
            long ˊ = edn.ˋ().ˊ();
            if (ˊ <= ˊˊ) {
                return false;
            }
            ˋ("excessiveTraffic#" + this.ˋॱ + "#" + ˊ);
            return true;
        }
    }

    private void ˏ(int i, int i2) {
        int ˋ = edt.ॱ().ˋ(this.ˎ.getApplicationContext(), -1);
        if (ˋ == 1) {
            try {
                Thread.sleep(((long) i) * 1000);
            } catch (InterruptedException e) {
                Logger.e("PreDlManThd", "sleep, e is: " + e.toString());
            }
        } else if (ˋ == 2) {
            try {
                Thread.sleep(((long) i2) * 1000);
            } catch (InterruptedException e2) {
                Logger.e("PreDlManThd", "sleep, e is: " + e2.toString());
            }
        } else {
            Logger.ˎ("PreDlManThd", "can not getThermalInfo :" + ˋ);
        }
    }

    private List<String> ˏ() {
        List<String> arrayList = new ArrayList();
        for (SessionDownloadTask ʿ : edp.ˏ().ˋ()) {
            arrayList.add(ʿ.ʿ());
        }
        return arrayList;
    }

    private void ˊ(SessionDownloadTask sessionDownloadTask) {
        SessionDownloadTask ˋ = dvt.ˏ().ˋ(sessionDownloadTask.ˊˊ());
        if (ˋ == null || (ˋ.ˌ() == this.ॱ.ॱ() && ˋ.ʾ() == sessionDownloadTask.ʾ())) {
            this.ॱ.ˊ(ˏ());
            edp.ˏ().ˎ(sessionDownloadTask);
            int ـ = sessionDownloadTask.ـ();
            Logger.ˎ("PreDlManThd", "begin run a DownloadRunnable name:" + sessionDownloadTask.ʿ() + ",pkg:" + sessionDownloadTask.ˊˊ() + ",reportStatus:" + ـ);
            if (!(ـ == 1 || TextUtils.isEmpty(sessionDownloadTask.ॱˋ()))) {
                ᐝ(sessionDownloadTask);
            }
            sessionDownloadTask.ˋ(1);
            djy.ˋ(1, new c(this));
            sessionDownloadTask.ˋ(new dvy());
            if (ˎ(this.ˎ)) {
                ـ = edt.ॱ().ˋ(this.ˎ, 0);
                int ˋ2 = edt.ॱ().ˋ(this.ˎ, 9);
                long currentTimeMillis = System.currentTimeMillis();
                if (ˎ(sessionDownloadTask, ˋ, false)) {
                    while (!dkb.ˏ(sessionDownloadTask.ˈ())) {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            Logger.ˎ("PreDlManThd", "doPreDownload InterruptedException");
                        }
                    }
                } else {
                    Logger.ˎ("PreDlManThd", "task is going wrong.");
                }
                int ˋ3 = edt.ॱ().ˋ(this.ˎ, 0);
                int ˋ4 = edt.ॱ().ˋ(this.ˎ, 9);
                edj ˋ5 = edh.ˋ(this.ˎ);
                long currentTimeMillis2 = System.currentTimeMillis();
                LinkedHashMap linkedHashMap = new LinkedHashMap(7);
                linkedHashMap.put("packageName", sessionDownloadTask.ˊˊ());
                linkedHashMap.put("cpuTemp", ـ + "#" + ˋ3);
                linkedHashMap.put("batteryTemp", ˋ2 + "#" + ˋ4);
                linkedHashMap.put("dTimes", String.valueOf(currentTimeMillis2 - currentTimeMillis));
                linkedHashMap.put(UpdateKey.STATUS, String.valueOf(sessionDownloadTask.ˈ()));
                linkedHashMap.put("alreadySize", String.valueOf(sessionDownloadTask.ˋˊ()));
                linkedHashMap.put("batteryStatus", ˋ5.ॱ + "#" + ˋ5.ˎ);
                cdl.ˎ("054", linkedHashMap);
                Logger.ˎ("PreDlManThd", "end run a pretask name:" + sessionDownloadTask.ʿ());
                return;
            }
            Logger.ˎ("PreDlManThd", "skip download runnable.....network is not wifi.");
            return;
        }
        Logger.ˎ("PreDlManThd", "task already exist:" + ˋ.ˊˊ() + "-" + ˋ.ʾ() + "-" + ˋ.ˌ());
    }

    private boolean ˎ(SessionDownloadTask sessionDownloadTask, SessionDownloadTask sessionDownloadTask2, boolean z) {
        if (sessionDownloadTask2 == null) {
            if (ˏ(sessionDownloadTask)) {
                return z;
            }
            return dvt.ˏ().ʽ(sessionDownloadTask);
        } else if (ˋ(sessionDownloadTask2)) {
            return z;
        } else {
            return dvt.ˏ().ˊ(sessionDownloadTask.ʾ());
        }
    }

    private boolean ˋ(SessionDownloadTask sessionDownloadTask) {
        if (sessionDownloadTask.ꓸ() == 0) {
            List ᐝˊ = sessionDownloadTask.ᐝˊ();
            if (!CollectCheck.isEmpty(ᐝˊ)) {
                SplitTask splitTask = (SplitTask) ᐝˊ.get(0);
                if (!(splitTask == null || splitTask.ʻ() <= 0 || edw.ˊ(splitTask.ʽ()))) {
                    String str = "doPreDownload reportErrorMsg4Enqueue|" + splitTask.ᐝ() + '|' + splitTask.ʼ() + '|' + splitTask.ʽ() + '|' + splitTask.ʻ() + '|' + splitTask.ॱˋ() + '|' + splitTask.ʼॱ();
                    LinkedHashMap linkedHashMap = new LinkedHashMap();
                    linkedHashMap.put(HianalyticsData.ERROR_CODE, "004");
                    linkedHashMap.put("error_desc", str);
                    cdl.ˎ("055", linkedHashMap);
                    dvt.ˏ().ॱ(sessionDownloadTask.ʾ());
                    return true;
                }
            }
        }
        return false;
    }

    private boolean ˏ(SessionDownloadTask sessionDownloadTask) {
        if (sessionDownloadTask.ꓸ() == 0) {
            List ᐝˊ = sessionDownloadTask.ᐝˊ();
            if (!CollectCheck.isEmpty(ᐝˊ)) {
                SplitTask splitTask = (SplitTask) ᐝˊ.get(0);
                if (!(splitTask == null || splitTask.ʻ() <= 0 || edw.ˊ(splitTask.ʽ()))) {
                    String str = "doPreDownload reportErrorMsg4Enqueue|" + splitTask.ᐝ() + '|' + splitTask.ʼ() + '|' + splitTask.ʽ() + '|' + splitTask.ʻ();
                    LinkedHashMap linkedHashMap = new LinkedHashMap();
                    linkedHashMap.put(HianalyticsData.ERROR_CODE, "003");
                    linkedHashMap.put("error_desc", str);
                    cdl.ˎ("055", linkedHashMap);
                    return true;
                }
            }
        }
        return false;
    }

    private void ᐝ(SessionDownloadTask sessionDownloadTask) {
        cvt o_dvo = new dvo(sessionDownloadTask, this.ॱ.ॱ());
        ResponseBean ॱ = cfw.ॱ(o_dvo);
        if (ॱ.getResponseCode() != 0) {
            Logger.e("PreDlManThd", "response code =" + ॱ.getResponseCode());
            return;
        }
        if (!sessionDownloadTask.ꜟ()) {
            StartDownloadResponse startDownloadResponse = (StartDownloadResponse) ॱ;
            if (!(TextUtils.isEmpty(o_dvo.pkgName_) || TextUtils.isEmpty(startDownloadResponse.appProfileUrl_) || TextUtils.isEmpty(startDownloadResponse.profileSha256_))) {
                fhz ˋ = fho.ˏ().ˋ("PackageManager");
                if (ˋ != null) {
                    cnt o_cnt = (cnt) ˋ.ॱ(cnt.class);
                    if (o_cnt != null) {
                        o_cnt.ˊ(ContentManager.getInstant().getContext(), o_dvo.pkgName_, o_dvo.versionCode, startDownloadResponse.appProfileUrl_, startDownloadResponse.profileSha256_);
                    }
                }
            }
            List list = startDownloadResponse.sliceCheckInfoSha256_;
            if (!(list == null || list.isEmpty())) {
                int i = 0;
                while (i < list.size()) {
                    try {
                        String[] split = ((DownloadChkInfo) list.get(i)).ˊ().split("-");
                        if (split.length == 2) {
                            ((DownloadChkInfo) list.get(i)).ˏ(Long.parseLong(split[0]));
                            ((DownloadChkInfo) list.get(i)).ˋ(Long.parseLong(split[1]));
                            ((DownloadChkInfo) list.get(i)).ॱ(0);
                        }
                        i++;
                    } catch (IndexOutOfBoundsException e) {
                        Logger.ˋ("PreDlManThd", "notifyResult" + e.toString());
                    }
                }
                List ᐝˊ = sessionDownloadTask.ᐝˊ();
                if (!CollectCheck.isEmpty(ᐝˊ)) {
                    ((SplitTask) ᐝˊ.get(0)).ˋ(list);
                }
            }
        }
        sessionDownloadTask.ˊॱ(1);
    }

    private void ˊ() {
        int ʽ;
        Logger.ˏ("PreDlManThd", "start monitor network quality");
        if (eqp.ᐝ()) {
            ʽ = edl.ˏ().ʽ();
        } else {
            ʽ = edl.ˏ().ˊॱ();
        }
        this.ˏ.ˏ(null, new bmx(ʽ, edl.ˏ().ʻ(), (int) edl.ˏ().ᐝ()));
    }

    private void ॱॱ() {
        Logger.ˏ("PreDlManThd", "stop monitor network quality");
        this.ˏ.ˎ();
    }

    private void ᐝ() {
        if (VERSION.SDK_INT >= 24) {
            this.ᐝ = new d();
            Builder builder = new Builder();
            builder.addCapability(12);
            NetworkRequest build = builder.build();
            ConnectivityManager connectivityManager = (ConnectivityManager) this.ˎ.getSystemService(ConnectivityManager.class);
            if (connectivityManager != null) {
                connectivityManager.registerNetworkCallback(build, this.ᐝ);
            }
        } else {
            this.ʼ = new b();
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
            ActivityUtil.registerReceiver(this.ˎ, intentFilter, this.ʼ);
        }
        edp.ˏ().ˋ().clear();
        ˊ.put(Integer.valueOf(this.ॱ.ॱ()), new eeb());
        this.ॱ.ˏ();
        Logger.ˎ("PreDlManThd", "begin PreDownloadManagerThread!");
    }

    private void ʻ() {
        int i;
        try {
            if (VERSION.SDK_INT >= 24) {
                ConnectivityManager connectivityManager = (ConnectivityManager) this.ˎ.getSystemService(ConnectivityManager.class);
                if (connectivityManager != null) {
                    connectivityManager.unregisterNetworkCallback(this.ᐝ);
                }
            } else {
                ActivityUtil.unregisterReceiver(this.ˎ, this.ʼ);
            }
        } catch (Throwable e) {
            Logger.ˋ("PreDlManThd", "unregisterReceiver error, e: ", e);
        }
        this.ॱ.ʼ();
        edp.ˏ().ˎ(null);
        edp.ˏ().ˋ().clear();
        djy.ॱ(1);
        eeb o_eeb = (eeb) ˊ.get(Integer.valueOf(this.ॱ.ॱ()));
        if (o_eeb != null) {
            while (this.ॱॱ < 180000 && o_eeb.ˏ.get() > 0) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e2) {
                    Logger.ˎ("PreDlManThd", "wait interrupted");
                }
                this.ॱॱ += 2000;
            }
            i = o_eeb.ॱ.get();
        } else {
            i = 0;
        }
        this.ॱ.ˊ(i);
        Logger.ˎ("PreDlManThd", "end PreDownloadManagerThread!");
    }

    private void ˏ(SessionDownloadTask sessionDownloadTask, int i, ApkUpgradeInfo apkUpgradeInfo) {
        int ˏ = etn.ˏ(i) | 8;
        if (!edl.ˏ().ˊˋ()) {
            ˏ |= 32;
        }
        if (!edl.ˏ().ˊᐝ()) {
            ˏ |= 64;
        }
        if (edm.ˏ(ContentManager.getInstant().getContext()).ˎ().contains(sessionDownloadTask.ˊˊ())) {
            ˏ |= DnsConfig.MAX_CACHE_ENTRIES;
        }
        ˏ(sessionDownloadTask, apkUpgradeInfo, ˏ);
    }

    private void ˏ(SessionDownloadTask sessionDownloadTask, ApkUpgradeInfo apkUpgradeInfo, int i) {
        coi o_edz = new edz(apkUpgradeInfo, this.ॱ.ॱ());
        fhz ˋ = fho.ˏ().ˋ("PackageManager");
        if (ˋ != null) {
            cnw o_cnw = (cnw) ˋ.ॱ(cnw.class);
            if (o_cnw != null) {
                List<SplitTask> ᐝˊ = sessionDownloadTask.ᐝˊ();
                if (!CollectCheck.isEmpty((List) ᐝˊ)) {
                    o.cof.d ˏ = new o.cof.d().ˋ(sessionDownloadTask.ˊˊ()).ˏ(sessionDownloadTask.ॱᐝ()).ˊ(i).ˊ(coc.UNCONCERN).ˏ((Object) sessionDownloadTask).ˏ(o_edz);
                    for (SplitTask splitTask : ᐝˊ) {
                        ˏ.ॱ(splitTask.ॱˊ(), splitTask.ॱᐝ(), splitTask.ᐝॱ());
                    }
                    cof ˎ = ˏ.ˎ();
                    eeb o_eeb = (eeb) ˊ.get(Integer.valueOf(this.ॱ.ॱ()));
                    if (o_eeb != null) {
                        o_eeb.ˏ.incrementAndGet();
                    }
                    o_cnw.ˋ(ContentManager.getInstant().getContext(), ˎ);
                    return;
                }
                return;
            }
            Logger.e("PreDlManThd", "can not found IPackageInstaller Api");
            return;
        }
        Logger.e("PreDlManThd", "can not found PackageManager module");
    }

    @Nullable
    private ApkUpgradeInfo ʽ(SessionDownloadTask sessionDownloadTask) {
        for (ApkUpgradeInfo apkUpgradeInfo : this.ˋ) {
            if (apkUpgradeInfo.ᐝ().equals(sessionDownloadTask.ˊˊ())) {
                return apkUpgradeInfo;
            }
        }
        return null;
    }
}
